<?php
# ***** BEGIN LICENSE BLOCK *****
# This file is a contributed part of DotClear.
# Copyright (c) 2005 Boris de Laage. All rights reserved.
#
# DotClear is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# DotClear is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with DotClear; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
# ***** END LICENSE BLOCK *****

define ('ASC_WEBSERVICES_URL', 'http://ws.audioscrobbler.com/1.0/user/%s/recenttracks.xml');

class dcScrobblerPlugin {

	var $_xml = NULL;
	var $_prefs;

	var $_data = NULL;

	
	function dcScrobblerPlugin() {
		$this->_prefs = iniFile::read(DC_SHARE_DIR.'/dcscrobbler/dcscrobbler.ini', true);
	}
	
	function getInstance() {
		static $instance;
		if (!isset($instance)) {
			$c = __CLASS__;
			$instance = new $c();
		}
		return $instance;
	}

	function getUserName() {
		return $this->_prefs['username'];
	}
	
	/*
	function getMaxLineLength() {
		return $this->_prefs['max_line_length'];
	}*/
	

	function getTracks() {
		$cache = DC_SHARE_DIR.'/dcscrobbler/cache.dat';
		if ($this->_prefs['username'] == '')
			return NULL;
		if (empty($this->_data)) {
			/* reload data if the cache :
				- does not exist
				- has expired (10 minutes)
				- must be skipped */
			if (! file_exists($cache) || (filemtime($cache) < (time() - $this->_prefs['cache_expires']))
									  || $this->_prefs['skip_cache']) {
				$this->_getData();
				return $this->_data;
			}
			$this->_data = unserialize(file_get_contents($cache));
		}
		return $this->_data;
	}

	/* Fetch data from AudioScrobbler's webservice */
	function _getData() {
		$cache = DC_SHARE_DIR.'/dcscrobbler/cache.dat';
		files::copyRemote(sprintf(ASC_WEBSERVICES_URL,$this->_prefs['username']), $cache.'.xml');
		if (!file_exists($cache.'.xml'))
			return false;
		
		$this->_xml = xml_parser_create('UTF-8');
		$this->_data = array();
		xml_parser_set_option($this->_xml, XML_OPTION_CASE_FOLDING, false);
		xml_parser_set_option($this->_xml,XML_OPTION_SKIP_WHITE, true);
		xml_set_object($this->_xml, $this);
		xml_set_element_handler($this->_xml,'_openTag','_closeTag');
		xml_set_character_data_handler($this->_xml, '_cdata');
		
		xml_parse($this->_xml,implode('',file($cache.'.xml')));
		
		/*clean*/
		unlink($cache.'.xml');
		unset($this->_current_cdata);
		xml_parser_free($this->_xml);
		
		/* serialize */
		$fp = fopen ($cache, 'w');
		fwrite($fp, serialize($this->_data));
		fclose($fp);
		
		return true;
	}
	
	function _openTag($p, $tag, $attr) {
		switch ($tag) {
			case 'track':
				$this->_track_info = array(
						'artist'	=> NULL,
						'name'		=> NULL,
						'url'		=> NULL,
						'date'		=> NULL
						);
				break;
			case 'date':
				if (!empty($attr['uts']))
					$this->_track_info['date'] = $attr['uts'];
				break;
		}
	}
	
	function _closeTag($p, $tag) {
        $this->_current_cdata = trim($this->_current_cdata);
		switch ($tag) {
			/* array keys & tags have the same name */
			case 'artist':
			case 'name':
			case 'url':
				$this->_track_info[$tag] = $this->_current_cdata;
				break;
			case 'track':
				array_push($this->_data, $this->_track_info);
				unset($this->_track_info);
				break;
		}
		unset ($this->_current_cdata);
	}
	
	function _cdata($p, $cdata) {
		$this->_current_cdata .= $cdata;
	}

}
?>